<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="球场老板" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入球场老板"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="球场名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入球场名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['football:stadium:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['football:stadium:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['football:stadium:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['football:stadium:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stadiumList"
      @selection-change="handleSelectionChange"
    >
    
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="props">
          <!-- @selection-change="handleSelectionChange" -->

          <!-- 球场详情 -->
           <stadiumDetail :stadiumList="props.row.stadiumDetailList"></stadiumDetail>
             <!-- <el-table  :data="props.row.stadiumDetailList">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="球场id" align="center" prop="stadiumId" />
            <el-table-column label="场地名称" align="center" prop="name" />
            <el-table-column label="一小时价钱" align="center" prop="hourPrice" />
            <el-table-column label="间距多少个小时开放" align="center" prop="spacing" />
            <el-table-column label="该场地的累计收入" align="center" prop="countPrice" />
            <el-table-column label="场地图片" align="center" prop="image" width="100">
              <template slot-scope="scope">
                <image-preview :src="scope.row.image" :width="50" :height="50"/>
              </template>
            </el-table-column>
            <el-table-column label="场地预定次数" align="center" prop="payNum" />
            <el-table-column label="球场类型id" align="center" prop="gameTypeId">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.game_type" :value="scope.row.gameTypeId"/>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['football:stadiumDetail:edit']"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['football:stadiumDetail:remove']"
                >删除</el-button>
              </template>
            </el-table-column>
    </el-table> -->
        </template>
      </el-table-column>



      <!-- <el-table-column label="${comment}" align="center" prop="id" /> -->
      <el-table-column
        label="球场老板"
        align="center"
        prop="userId"
      />
      <el-table-column label="球场被预定的次数" align="center" prop="payNum" />
      <el-table-column label="球场联系号码" align="center" prop="phone" />
      <el-table-column label="球场累计收益" align="center" prop="countPrice" />
      <!-- <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" /> -->
      <el-table-column label="球场首图" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="球场名字" align="center" prop="name" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="县" align="center" prop="county" />
      <el-table-column label="详细地址"  width="280" align="center" prop="address" />
      <!-- <el-table-column label="省编码" align="center" prop="provinceCode" />
      <el-table-column label="市编码" align="center" prop="cityCode" />
      <el-table-column label="县编码" align="center" prop="countyCode" /> -->
      <el-table-column
        label="营业开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="营业结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
      
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['football:stadium:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['football:stadium:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-add"
            @click="addOrEditStadiumDetail(scope.row.id)"
            >添加球场详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改球场对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="球场老板" prop="userId">
          <el-input
            v-model="form.userId"
            placeholder="请输入球场老板id"
          />
        </el-form-item>
        <!-- <el-form-item label="球场被预定的次数" prop="payNum">
          <el-input
            v-model="form.payNum"
            placeholder="请输入球场被预定的次数"
          />
        </el-form-item> -->
        <el-form-item label="球场联系号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入球场联系号码" />
        </el-form-item>
        <!-- <el-form-item  label="球场累计收益" prop="countPrice">
          <el-input
            v-model="form.countPrice"
            placeholder="请输入球场累计收益"
          />
        </el-form-item> -->
        <!-- <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item> -->
        <el-form-item label="球场首图" prop="image">
          <image-upload v-model="form.image" />
        </el-form-item>
        <el-form-item label="球场名字" prop="name">
          <el-input v-model="form.name" placeholder="请输入球场名字" />
        </el-form-item>
        <!-- <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省" />
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市" />
        </el-form-item>
        <el-form-item label="县" prop="county">
          <el-input v-model="form.county" placeholder="请输入县" />
        </el-form-item> -->
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
          <i @click="setVisible(true)" class="el-icon-position"></i>
        </el-form-item>
        <!-- <el-form-item label="省编码" prop="provinceCode">
          <el-input v-model="form.provinceCode" placeholder="请输入省编码" />
        </el-form-item>
        <el-form-item label="市编码" prop="cityCode">
          <el-input v-model="form.cityCode" placeholder="请输入市编码" />
        </el-form-item>
        <el-form-item label="县编码" prop="countyCode">
          <el-input v-model="form.countyCode" placeholder="请输入县编码" />
        </el-form-item> -->
        <el-form-item label="营业时间" prop="startTime">
          <el-time-picker
            is-range
            v-model="timeArr"
            format="HH:mm"
            start-placeholder="营业时间"
            end-placeholder="结束时间"
            placeholder="请选择营业开始时间范围"
            @change="timeChange()"
          >
          </el-time-picker>
        </el-form-item>
        <!-- <el-form-item label="营业结束时间" prop="endTime">
          <el-date-picker
            clearable
            v-model="form.endTime"
            type="datetime"
            value-format="yyyy-MM-dd"
            placeholder="请选择营业结束时间"
          >
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    
    <!-- 球场添加或者修改弹窗 -->
    <!-- <stadiumAddEdit :isOpen="isOpen" :stadiumId="stadiumId" @handlerOpen="handlerOpen"></stadiumAddEdit> -->
    <!-- 地图组件 -->
    <!-- <tencent-map
      :visible="visible"
      @setVisible="setVisible"
      :selected-values="selectedValue"
      @selected-change="selectedChange"
    /> -->
  </div>
</template>

<script>
import { listStadium, getStadium, delStadium, addStadium, updateStadium } from "@/api/football/stadium";
import TencentMap from "@/components/Map/TencentMap";
import stadiumAddEdit from '../stadiumDetail/stadiumAddEdit'
import stadiumDetail from '../stadiumDetail/index'

export default {
  name: "Stadium",
  components: {
    TencentMap,
    stadiumAddEdit,
    stadiumDetail
  },
  dicts: ['game_type'],

  data() {
    return {
      stadiumId:0,
      isOpen:false,
      timeArr:[null,null],
      visible:false,
      selectedValue:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 球场表格数据
      stadiumList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        address: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "球场老板id，关联微信用户表的id不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
 
  methods: {
    // 修改弹窗状态
    handlerOpen(){
      this.isOpen= false
    },
    addOrEditStadiumDetail(stadiumId){
      this.isOpen= true
      console.log(this.isOpen,stadiumId,'stadiumId');
      this.stadiumId = stadiumId
    },
    // 时间选择
    timeChange(e){
      console.log(this.timeArr,'===');
      const formattedTimeArray = this.timeArr.map(date => {  
       const hours = date.getHours().toString().padStart(2, '0'); // 获取小时，并补零  
      const minutes = date.getMinutes().toString().padStart(2, '0'); // 获取分钟，并补零  
      return `${hours}:${minutes}`; // 返回格式化后的时间  
  });  

console.log(formattedTimeArray,'shijian');
this.form.startTime = formattedTimeArray[0]
this.form.endTime = formattedTimeArray[1]

    },  
     setVisible(visible){
      this.visible=visible
    },
    // 地图回传
    selectedChange(value,staduimName){
      if (staduimName) {
        this.form.name = staduimName
      }
      if (value) {
        this.form.address = value.address + ' ' + staduimName;
        this.form.province = value.province
        this.form.city = value.city
        this.form.county = value.district
        this.form.latitude = value.location.lat
        this.form.longitude = value.location.lng
        // this.form.countyCode = value.province
        // this.form.province = value.province
      }
      console.log(value,"_--------------------",this.form)
    },
    /** 查询球场列表 */
    getList() {
      this.loading = true;
      listStadium(this.queryParams).then(response => {
        this.stadiumList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        payNum: null,
        phone: null,
        countPrice: null,
        longitude: null,
        latitude: null,
        image: null,
        name: null,
        province: null,
        city: null,
        county: null,
        address: null,
        provinceCode: null,
        cityCode: null,
        countyCode: null,
        startTime: null,
        endTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加球场";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStadium(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改球场";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStadium(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStadium(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除球场编号为"' + ids + '"的数据项？').then(function() {
        return delStadium(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('football/stadium/export', {
        ...this.queryParams
      }, `stadium_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
